using System.Web.UI;

namespace Rioshu.Web.ViewStateHandlers
{
    public class ViewStatePersister : PageStatePersister
    {
        public ViewStatePersister(Page page) : base(page)
        {
            ViewStatePersistersConfigurationHandler configuration =
                ViewStatePersistersConfigurationHandler.GetConfiguration();
            Persister = PersisterLoader.LoadPersister(configuration.DefaultPersisterType);
        }

        protected IViewStateHandlersPersister Persister { get; set; }

        public override void Load()
        {
            Pair pair = Persister.LoadState(Page);
            ViewState = pair.First;
            ControlState = pair.Second;
        }

        public override void Save()
        {
            if (ViewState == null || ControlState == null) return;

            var state = new Pair(ViewState, ControlState);
            Persister.SaveState(Page, state);
        }
    }
}